<template>
  <div class="result-container">
    <div class="title-wrap">
      <!-- 标题 -->
      <h2 class="title">{{ $route.query.keywords }}</h2>
      <span class="sub-title">找到 {{ count }} 个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              v-for="(item, index) in songList"
              :key="index"
              class="el-table__row"
              @dblclick="stopMusic"
            >
              <td>{{ index + 1 }}</td>

              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span
                      class="iconfont"
                      :class="
                        id == item.id
                          ? 'el-icon-video-pause'
                          : 'el-icon-video-play'
                      "
                      @click="playMusic(item.id)"
                    ></span>
                    <!-- 歌名 -->
                    <span>{{ item.name }}</span>
                    <!-- mv的图标 -->
                    <span
                      v-if="item.mvid != 0"
                      class="iconfont icon-mv"
                      @click="goMv(item.mvid)"
                    ></span>
                  </div>
                  <!-- 二级标题 -->
                  <span v-if="item.alias.length != 0">{{ item.alias[0] }}</span>
                </div>
              </td>
              <td>{{ item.artists[0].name }}</td>
              <td>{{ item.album.name }}</td>
              <td>{{ item.duration | timeForm }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div
            v-for="(item, index) in playlists"
            :key="index"
            class="item"
            @click="toPlaylist(item.id)"
          >
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{ item.playCount }}</span>
              </div>
              <img :src="item.coverImgUrl" alt="" />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{ item.name }}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div
            v-for="(item, index) in mv"
            :key="index"
            class="item"
            @click="toMV(item.id)"
          >
            <div class="img-wrap">
              <!-- 封面 -->
              <img :src="item.cover" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <!-- 播放次数 -->
                <div class="num">{{ item.playCount }}</div>
              </div>
              <!-- 持续时间 -->
              <span class="time">{{ item.duration | timeForm }}</span>
            </div>
            <div class="info-wrap">
              <!-- mv名 -->
              <div class="name">{{ item.name }}</div>
              <!-- 歌手名 -->
              <div class="singer">{{ item.artistName }}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页器
      total 总条数
      current-page 当前页
      page-size 每页多少条数据
      current-change 页码改变事件
     -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="count"
      :current-page="page"
      :page-size="limit"
    ></el-pagination>
  </div>
</template>

<script>
// 导入 aixos
import axios from "axios";
export default {
  data() {
    return {
      // tab切换时 会改变的数据
      activeIndex: "songs",
      // 保存 查询歌曲
      songList: [],
      // 保存歌单的字段
      playlists: [],
      // 保存mv的字段
      mv: [],
      // 搜索结果的总数
      count: 0,
      //控制播放状态的icon
      id: "",
      page: 1,
      count: 0,
      type: 1,
      limit: 10,
      songName: "",
    };
  },
  // 生命周期钩子 回调函数
  mounted() {
    this.getSongs();
  },

  // 方法
  methods: {
    //获取歌曲
    getSongs() {
      this.songName = this.$route.query.keywords;
      axios({
        url: "https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.$route.query.keywords,
          type: this.type,
          // 获取的数据量
          limit: this.limit,
          offset: (this.page - 1) * 10,
        },
      }).then((res) => {
        console.log(res);
        this.songList = res.data.result.songs;
        // 保存总数
        this.count = res.data.result.songCount;
      });
    },
    // 去mv详情页
    toMV(id) {
      this.$router.push(`/mv?id=${id}`);
    },
    // 去歌单详情页
    toPlaylist(id) {
      // 跳转并携带数据即可
      this.$router.push(`/playlist?id=${id}`);
    },
    playMusic(id) {
      this.id = id;
      axios({
        url: "https://autumnfish.cn/song/url",
        method: "get",
        params: {
          id, // id:id
        },
      }).then((res) => {
        // console.log(res)
        let url = res.data.data[0].url;
        console.log(this.$parent);
        // 设置给父组件的 播放地址
        this.$parent.url = url;
      });
    },
    stopMusic() {
      this.id = "";
      this.$parent.url = "";
    },
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.watchChange();
    },
    goMv(id) {
      this.$router.push("/mv?id=" + id);
    },
    //监听tab栏变化的操作
    watchChange() {
      // songs 歌曲
      // lists 歌单
      // mv    mv
      // 获取个数
      let limit = 10;
      switch (this.activeIndex) {
        case "songs":
          this.type = 1;
          limit = 10;
          break;
        case "lists":
          this.type = 1000;
          limit = 10;
          break;
        case "mv":
          this.type = 1004;
          limit = 8;
          break;

        default:
          break;
      }
      // console.log(this.page)
      axios({
        url: "https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.$route.query.keywords,
          type: this.type, // type:type,
          // 获取的数据量
          limit, // limit: limit
          offset: (this.page - 1) * limit,
        },
      }).then((res) => {
        console.log(res);
        // 获取歌曲
        if (this.type == 1) {
          // 歌曲
          this.songList = res.data.result.songs;
          // 保存总数
          this.count = res.data.result.songCount;
        }
        // 获取 歌单
        else if (this.type == 1000) {
          // 歌单的逻辑
          this.playlists = res.data.result.playlists;
          // 总数
          this.count = res.data.result.playlistCount;
          // 处理 播放次数
          for (let i = 0; i < this.playlists.length; i++) {
            if (this.playlists[i].playCount > 100000) {
              this.playlists[i].playCount =
                parseInt(this.playlists[i].playCount / 10000) + "万";
            }
          }
        } else {
          // 保存mv数据
          this.mv = res.data.result.mvs;
          // 总数
          this.count = res.data.result.mvCount;
          // 处理数据
          for (let i = 0; i < this.mv.length; i++) {
            // 播放次数
            if (this.mv[i].playCount > 100000) {
              this.mv[i].playCount =
                parseInt(this.mv[i].playCount / 10000) + "万";
            }
          }
        }
      });
    },
  },
  // 侦听器
  watch: {
    activeIndex() {
      //先把page重置为1
      this.page = 1;
      this.watchChange();
    },
    //搜索时监听路由的值，进行重新获取数据
    $route: function() {
      this.getSongs();
    },
  },
};
</script>

<style>
.el-icon-video-play {
  margin-right: 6px;
}
</style>
